package org.hiside.android.travel.dashboard.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import org.hiside.android.travel.dashboard.DashboardActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

public class DrawView extends View {

    protected static final String TAG = DrawView.class.getSimpleName();

    int x0, y0; // 用于保存拖动时的上一个点的位置

    Point po = new Point(); // 圆心（坐标值是相对与控件的左上角的）

    int centerX, centerY; // 控件的中心点

    public DrawView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setAntiAlias(true);

        // paint main line
        p.setColor(Color.GREEN);
        p.setTextSize(26);
        canvas.drawLine(10, 0, 10, getHeight(), p);

        String[] data1 = { "2012-11-21 08:00", "集合：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项1：xxxxxx" + Calendar.getInstance().getTime() };
        String[] data2 = { "2012-11-21 09:00", "出发：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项2：xxxxxx" + Calendar.getInstance().getTime() };
        String[] data3 = { "2012-11-21 12:00", "中午休息：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项3：xxxxxx" + Calendar.getInstance().getTime() };
        String[] data4 = { "2012-11-21 14:00", "游览京津冀：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项4：xxxxxx" + Calendar.getInstance().getTime() };
        String[] data5 = { "2012-11-21 17:00", "晚饭：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项5：xxxxxx" + Calendar.getInstance().getTime() };
        String[] data6 = { "2012-11-21 20:00", "篝火晚会：" + Calendar.getInstance().getTime(),
                "参观景点XXXX：" + Calendar.getInstance().getTime(), "注意事项6：xxxxxx" + Calendar.getInstance().getTime() };

        drawNode(canvas, 10, 25, 0, 25, p, data1);
        drawNode(canvas, 10, 200, 0, 200, p, data2);
        drawNode(canvas, 10, 400, 0, 400, p, data3);
        drawNode(canvas, 10, 600, 0, 600, p, data4);
        drawNode(canvas, 10, 800, 0, 800, p, data5);
        drawNode(canvas, 10, 1000, 0, 1000, p, data6);

        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();

    }

    public void createSDCardDir(Context context) {
        if (getSDPath() == null) {
            Toast.makeText(context, "未找到SD卡", Toast.LENGTH_LONG).show();
        } else {
            // 创建一个文件夹对象，赋值为外部存储器的目录
            File sdcardDir = Environment.getExternalStorageDirectory();
            // 得到一个路径，内容是sdcard的文件夹路径和名字
            String newPath = sdcardDir.getPath() + "/hitravel/";
            File path = new File(newPath);
            if (!path.exists()) {
                path.mkdirs();
                Log.i(TAG, "create sd card dir " + newPath);
            }
        }
    }

    public String getSDPath() {
        File SDdir = null;
        boolean sdCardExist = android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment
                .getExternalStorageState());
        if (sdCardExist) {
            SDdir = Environment.getExternalStorageDirectory();
        }
        if (SDdir != null) {
            return SDdir.toString();
        } else {
            return null;
        }
    }

    public static boolean existSDcard() {
        if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
            return true;
        } else
            return false;
    }

    private void drawNode(Canvas canvas, float startx, float starty, float endx, float endy, Paint p, String[] data) {
        p.setColor(Color.GREEN);
        canvas.drawLine(startx, starty, getWidth(), endy, p);

        p.setColor(Color.RED);
        canvas.drawCircle(startx, starty, 5, p);

        p.setColor(Color.BLACK);
        float textx = startx + 10;
        for (String d : data) {
            canvas.drawText(d, textx, starty, p);
            starty += 30;
        }
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        centerX = w / 2;
        centerY = h / 2;
        po.set(centerX, centerY);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /*
     * 拖动事件监听
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        /*
         * (x,y)点为发生事件时的点，它的坐标值为相对于该控件左上角的距离
         */
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (action) {

        case MotionEvent.ACTION_DOWN: // 按下
            x0 = x;
            y0 = y;
            Log.i("down", "(" + x0 + "," + y0 + ")");
            break;
        case MotionEvent.ACTION_MOVE: // 拖动
            /*
             * 拖动时圆心坐标相对运动 (x0,y0)保存先前一次事件发生的坐标 拖动的时候只要计算两个坐标的delta值，然后加到圆心中，即移动了圆心。
             * 然后调用invalidate()让系统调用onDraw()刷新以下屏幕，即实现了坐标移动。
             */
            po.x += x - x0;
            po.y += y - y0;
            x0 = x;
            y0 = y;
            Log.i("move", "(" + x0 + "," + y0 + ")");
            invalidate();
            break;
        case MotionEvent.ACTION_UP: // 弹起
            break;
        }

        /*
         * 注意：这里一定要返回true 返回false和super.onTouchEvent(event)都会本监听只能检测到按下消息
         * 这是因为false和super.onTouchEvent(event)的处理都是告诉系统该控件不能处理这样的消息， 最终系统会将这些事件交给它的父容器处理。
         */
        return true;
    }
}
